package com.gubatian.demo.algorithm.simple;

import java.util.HashSet;
import java.util.Set;

/**
 * 求两数之和
 * @author gubatian
 * @date 2021/9/8 10:50 上午
 */
public class SumToNums {
    public static Set<int[]> twoSum(int[] nums, int target) {
        Set<int[]> arrs = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            int srci = nums[i];
            if (srci < target) {
                for (int j = 0; j < nums.length; j++) {
                    int srcj = nums[j];
                    if (srcj > target) {
                        break;
                    }
                    if ((srci + srcj) == target) {
                        int[] arr = {i, j};
                        arrs.add(arr);
                    }
                }
            }
        }
        return arrs;

    }

    public static void main(String[] args) {
        int[] nums = {1,3,9,8,5,2};
        Set<int[]> arrs = twoSum(nums, 8);
        for (int[] arr : arrs) {
            for (int n : arr) {
                System.out.print(n + ",");
            }
        }
    }
}
